package com.daxt.controller.sysarchives;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;

import com.daxt.mapper.sys.ArchivesFondsMapper;
import com.daxt.mapper.sys.ArchivesTypeMapper;
import com.daxt.mapper.sys.UreportFileMapper;
import com.daxt.model.dto.BaseQueryDto;
import com.daxt.model.dto.ReportTemplateParam;
import com.daxt.model.service.archives.vo.ArchivesDoc;
import com.daxt.model.service.archives.vo.ArchivesType;
import com.daxt.model.service.base.vo.UreportFile;
import com.daxt.service.ArchivesTempletService;
import com.daxt.utils.ArchivesUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultCode;
import com.daxt.common.result.ResultUtil;
import com.daxt.mapper.archives.ArchivesTempletMapper;
import com.daxt.model.service.archives.vo.ArchivesTemplet;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "档案模板")
@RestController
public class ArchivesTempletController {

    @Autowired
    private ArchivesTempletMapper archivesTempletMapper;
    @Autowired
    private ArchivesTempletService archivesTempletService;
    @Autowired
    private ArchivesTypeMapper archivesTypeMapper;
    //	@Autowired
//	private TemplateMapper templateMapper;
    @Autowired
    private UreportFileMapper ureportFileMapper;
    @Autowired
    private ArchivesFondsMapper archivesFondsMapper;


    @ApiOperation(value = "获取列表", notes = "")
    @GetMapping("/archivesTemplet")
    public Result<List<ArchivesTemplet>> list(
            @ApiParam("门类ID") @RequestParam("typeId") String typeId,
            @ApiParam("全宗ID") @RequestParam("fondsId") String fondsId) {
        if (StringUtils.isEmpty(typeId)) {
            return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "", null);
        }

        QueryWrapper<ArchivesTemplet> queryWrapper = ArchivesUtil.builBaseQueryWrapper(new BaseQueryDto(fondsId, typeId));

        List<ArchivesTemplet> info = archivesTempletMapper.selectList(queryWrapper);
        info.forEach((ArchivesTemplet at) -> {

            UreportFile template = ureportFileMapper.selectById(at.getTempletId());
            at.setFileName(template.getName());
            at.setModel(template.getModel());
            at.setFileUrl(template.getUrl());
        });
        return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, info);
    }

    @ApiOperation(value = "添加", notes = "")
    @PostMapping("/archivesTemplet")
    public Result<String> save(@RequestBody ArchivesTemplet at) {
//		Template template = templateMapper.selectById(at.getTempletId());
//		at.setFileName(template.getName());
        int rows = archivesTempletMapper.insert(at);
        return ResultUtil.data(rows > 0 ? ResultCode.RESULT_SUCCESS.getCode() : ResultCode.RESULT_ERROR.getCode(), null, null);
    }

    @ApiOperation(value = "修改", notes = "")
    @PutMapping("/archivesTemplet")
    public Result<String> edit(@RequestBody ArchivesTemplet at) {
//		Template template = templateMapper.selectById(at.getTempletId());
//		at.setFileName(template.getName());
        int rows = archivesTempletMapper.updateById(at);
        return ResultUtil.data(rows > 0 ? ResultCode.RESULT_SUCCESS.getCode() : ResultCode.RESULT_ERROR.getCode(), null, null);
    }

    @ApiOperation(value = "删除", notes = "")
    @DeleteMapping("/archivesTemplet")
    public Result<String> del(@RequestBody @Valid @NotEmpty List<String> ids) {

        int rows = archivesTempletMapper.deleteBatchIds(ids);
        return ResultUtil.data(rows > 0 ? ResultCode.RESULT_SUCCESS.getCode() : ResultCode.RESULT_ERROR.getCode(), null, null);
    }


    @ApiOperation(value = "一键复制", notes = "")
    @PostMapping("/archivesTemplet/copys")
    @Transactional(rollbackFor = Exception.class)
    public Result<String> copys(@RequestBody @Valid @NotEmpty ReportTemplateParam rtp) {
        List<ArchivesTemplet> atList = new ArrayList<ArchivesTemplet>();
        QueryWrapper<ArchivesType> queryWrapper = new QueryWrapper<ArchivesType>();
        queryWrapper.in("id", rtp.getTypeId());
        List<ArchivesType> types = archivesTypeMapper.selectList(queryWrapper);


        rtp.getTemplateIds().forEach((String id) -> {
            types.forEach((ArchivesType atBean) -> {
                ArchivesTemplet atOne = archivesTempletMapper.selectById(id);
                ArchivesTemplet at = new ArchivesTemplet();
                BeanUtils.copyProperties(atOne, at);
                at.setFondsId(atBean.getFondsId());
                at.setFondsNum(atBean.getFondsNum());
                at.setTypeId(atBean.getId().toString());
                at.setTypeNum(atBean.getTypeNum());
                atList.add(at);
            });

        });


        boolean boo = archivesTempletService.saveBatch(atList);
        if (boo) return ResultUtil.success();
        return ResultUtil.error();


    }

}
